package com.chenyaowei.marketing.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.chenyaowei.common.utils.DateUtils;
import com.chenyaowei.marketing.domain.CywCouponInfo;
import com.chenyaowei.marketing.mapper.CywCouponInfoMapper;
import com.chenyaowei.marketing.service.ICywCouponInfoService;
import com.chenyaowei.marketing.service.ICywCouponSpuCategoryRelationService;
import com.chenyaowei.marketing.service.ICywCouponSpuRelationService;
import com.chenyaowei.member.domain.CywMemberStatisticsInfo;
import com.chenyaowei.member.service.ICywMemberStatisticsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.chenyaowei.common.utils.DateUtils.YYYY_MM_DD_HH_MM_SS;
import static com.chenyaowei.common.utils.SecurityUtils.getUserId;

/**
 * 优惠券信息Service业务层处理
 * 
 * @author 陈耀威
 * @date 2023-11-28
 */
@Service
public class CywCouponInfoServiceImpl implements ICywCouponInfoService 
{
    @Autowired
    private CywCouponInfoMapper cywCouponInfoMapper;

    @Autowired
    private ICywMemberStatisticsInfoService memberStatisticsInfoService;

    @Autowired
    private ICywCouponSpuRelationService couponSpuRelationService;

    @Autowired
    private ICywCouponSpuCategoryRelationService couponSpuCategoryRelationService;

    /**
     * 查询优惠券信息
     * 
     * @param id 优惠券信息主键
     * @return 优惠券信息
     */
    @Override
    public CywCouponInfo selectCywCouponInfoById(Long id)
    {
        return cywCouponInfoMapper.selectCywCouponInfoById(id);
    }

    /**
     * 查询优惠券信息列表
     * 
     * @param cywCouponInfo 优惠券信息
     * @return 优惠券信息
     */
    @Override
    public List<CywCouponInfo> selectCywCouponInfoList(CywCouponInfo cywCouponInfo)
    {
        return cywCouponInfoMapper.selectCywCouponInfoList(cywCouponInfo);
    }

    /**
     * 新增优惠券信息
     * 
     * @param cywCouponInfo 优惠券信息
     * @return 结果
     */
    @Override
    public int insertCywCouponInfo(CywCouponInfo cywCouponInfo)
    {
        return cywCouponInfoMapper.insertCywCouponInfo(cywCouponInfo);
    }

    /**
     * 修改优惠券信息
     * 
     * @param cywCouponInfo 优惠券信息
     * @return 结果
     */
    @Override
    public int updateCywCouponInfo(CywCouponInfo cywCouponInfo)
    {
        return cywCouponInfoMapper.updateCywCouponInfo(cywCouponInfo);
    }

    /**
     * 批量删除优惠券信息
     * 
     * @param ids 需要删除的优惠券信息主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteCywCouponInfoByIds(Long[] ids)
    {
        List<CywCouponInfo> list = cywCouponInfoMapper.selectListByIds(ids);
        List<Long> cateCouponIds = list.stream().filter(item -> item.getUseType().equals(1)).map(CywCouponInfo::getId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(cateCouponIds)) {
            couponSpuCategoryRelationService.deleteCywCouponSpuCategoryRelationByIds(cateCouponIds.toArray(new Long[cateCouponIds.size()]));
        }
        List<Long> spuCouponIds = list.stream().filter(item -> item.getUseType().equals(2)).map(CywCouponInfo::getId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(spuCouponIds)) {
            couponSpuRelationService.deleteCywCouponSpuRelationByIds(spuCouponIds.toArray(new Long[spuCouponIds.size()]));
        }
        return cywCouponInfoMapper.deleteCywCouponInfoByIds(ids);
    }

    /**
     * 删除优惠券信息信息
     * 
     * @param id 优惠券信息主键
     * @return 结果
     */
    @Override
    public int deleteCywCouponInfoById(Long id)
    {
        return cywCouponInfoMapper.deleteCywCouponInfoById(id);
    }

    @Override
    public List<CywCouponInfo> selectFrontList(CywCouponInfo cywCouponInfo) {
        List<CywCouponInfo> list = cywCouponInfoMapper.selectFrontList(cywCouponInfo);
        // 会员等级
        CywMemberStatisticsInfo cywMemberStatisticsInfo = memberStatisticsInfoService.selectInfoByMemberId(getUserId());
        list.forEach(item -> {
            if (item.getNum().equals(item.getReceiveCount())){
                item.setEnable(false);
                item.setMsg("无佐，伙计" );
                return;
            }
            if (item.getMemberLevel() > cywMemberStatisticsInfo.getMemberLevel()){
                item.setEnable(false);
                item.setMsg("限\"" + item.getMemberLevelName() + "\"及以上等级会员领取" );
                return;
            }
            if (item.getRequiredIntegral() > cywMemberStatisticsInfo.getIntegration()){
                item.setEnable(false);
                item.setMsg("积分不足" );
                return;
            }
            if (item.getEnableStartTime().after(new Date()) || item.getEnableEndTime().before(new Date())){
                item.setEnable(false);
                item.setMsg("领取时间：" + DateUtils.parseDateToStr(YYYY_MM_DD_HH_MM_SS, item.getEnableStartTime()) + "至" + DateUtils.parseDateToStr(YYYY_MM_DD_HH_MM_SS, item.getEnableEndTime()));
                return;
            }
            item.setEnable(true);
        });
        return list;
    }

    @Override
    public int subtractCouponNum(Long id) {
        return cywCouponInfoMapper.subtractCouponNum(id);
    }

    @Override
    public List<CywCouponInfo> selectListByIds(List<Long> couponIds) {
        return cywCouponInfoMapper.selectListByIds(couponIds.toArray(new Long[couponIds.size()]));
    }

    @Override
    public int updateCouponUseCount(Long couponId, Integer num) {
        return cywCouponInfoMapper.updateCouponUseCount(couponId, num);
    }
}
